//
//  DXFEntity.m
//  dxfreader
//
//  Created by Holmes Futrell on 5/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DXFNode.h"

@interface DXFNode()
@property (readwrite, assign) BOOL exists;
@end;

@implementation DXFNode

@synthesize groupCode, exists;

+(DXFNode *)nullNode {
	DXFNode *node = [[[ DXFNode alloc] initWithGroupId: 0 data:@"(null-node)"] autorelease ];
	node.exists = NO;
	return node;
}

-(id)initWithGroupId:(int)gId data:(NSString *)_data; {
	
	self = [super init];
	
	children = [[NSMutableArray alloc] init];	
	data = [_data copy];
	groupCode = gId;
	
	exists = YES;
	
	return self;
	
}

-(void)addChild:(DXFNode *)child {
	[children addObject: child];
}

-(NSString *)stringValue {
	if (
		( groupCode >= 0 && groupCode <= 9 ) ||
		( groupCode == 100 ) ||
		( groupCode == 102 ) ||
		( groupCode == 103 ) ||
		( groupCode == 105 ) ||
		( groupCode >= 300 && groupCode <= 309 ) ||
		( groupCode == 999 ) ||
		( groupCode >= 1000 && groupCode <= 1009 )
		) {
		
	}
	else {
		NSLog(@"Warning: getting string rep for group code %d", groupCode);
	}
	return data;
}

-(int)hexValue {
	if (
		( groupCode >= 320 && groupCode <= 329 ) ||
		( groupCode >= 330 && groupCode <= 369 )
		)
	{
		;
	}
	else {
		NSLog(@"Warning: hex rep for group code %d", groupCode);
	}
	unsigned result = 0;
	NSScanner *scanner = [NSScanner scannerWithString: data];
	[scanner setScanLocation:1]; // bypass '#' character
	[scanner scanHexInt:&result];
	return result;
}

-(int)intValue {
	
	if ( 
		( groupCode >= 60 && groupCode <= 79 ) ||
		( groupCode >= 90 && groupCode <= 99 ) ||
		( groupCode == 105 ) ||
		( groupCode >= 170 && groupCode <= 175 ) ||
		( groupCode >= 280 && groupCode <= 289 ) ||
		( groupCode >= 1060 && groupCode <= 1070 ) ||
		( groupCode == 1070 )) {
		;
	}
	else {
		NSLog(@"Warning: getting integer rep for group code %d", groupCode);
	}
	
	return [data intValue];
	
}

-(double)doubleValue {
	if ( 
		( groupCode >= 10 && groupCode <= 59 ) ||
		( groupCode >= 140 && groupCode <= 147 ) ||
		( groupCode >= 1010 && groupCode <= 1059) ||
		( groupCode == 210) ||
		( groupCode == 220) ||
		( groupCode == 230)		
		)
	{
		;
	}
	else {
		NSLog(@"Warning: getting double rep for group code %d", groupCode);
	}
	
	return [data doubleValue];
	
}


-(NSArray *)children {
	return [NSArray arrayWithArray: children];
}

-(DXFNode *)childOfCode:(int)_groupCode {

	for ( DXFNode *child in children ) {
		if ( child.groupCode == _groupCode ) {
			return child;
		}
	}
	return [DXFNode nullNode];
	
	
}

-(BOOL)childExistsForCode:(int)_groupCode {
	return [ self childOfCode: _groupCode ].exists;
}

-(NSString *)description {
	
	NSMutableString *s = [ NSMutableString stringWithCapacity: 0];
	
	[ s appendString: [ NSString stringWithFormat:@"<DXFNode groupId=%d, data=%@>\n", groupCode, data] ];
	
	if ([children count] > 0) {
		[ s appendString: @"<children>\n" ];
		for ( DXFNode *child in children ) {
			[s appendString: child.description ];
		}
		[ s appendString: @"\n<end children>" ];
	}
	
	return s;
}


@end
